# -*- coding: utf-8 -*- 
# @Time : 2023/11/28 16:07 
# @Author : YangGuangjian
# @File : sub_string.py

import re


def replace_placeholders(template, replacements):
    """
    批量替换模板中的占位符

    Parameters:
    - template (str): 包含占位符的模板字符串
    - replacements (dict): 包含要替换的占位符及其对应值的字典

    Returns:
    - str: 替换后的字符串
    """
    for placeholder, value in replacements.items():
        # 使用正则表达式替换字符串中的 ${placeholder} 占位符
        template = re.sub(r'\${' + re.escape(placeholder) + '}', str(value), template)

    return template


if __name__ == '__main__':
    template = "{'srid': ${srid}, 'name': ${name}}"
    replacements = {'srid': 123, 'name': 'John', 'name123': 'John123'}

    result = replace_placeholders(template, replacements)
    print(result)